<?php
/*
fileHashDB
Copyright (c) 2010 The Hursk Group, LLC. All rights reserved.

www.hursk.com

hurskgroup@hursk.com

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

*/
require 'db_functions.php';
require 'common_functions.php';

$start_time_epoch = time();

$file1 = "/tmp/nsrl/NSRLFile.txt";
$file2 = "/tmp/nsrl/NSRLMfg.txt";
$file3 = "/tmp/nsrl/NSRLOS.txt";
$file4 = "/tmp/nsrl/NSRLProd.txt";

// load config file	and check for local copy
if (file_exists("../local_config.php")) {
	require '../local_config.php';
} else if(file_exists("../config.php")) {
	require '../config.php';
} else {
	$xml = c_initiate_xml($config_data);
	$xml_error = $xml->addchild('error');
	$xml_error->addchild('code', '500');
	$xml_error->addchild('description', 'No config file found.');
	c_announce($xml);
}

// make db connection
$db_link = dbf_connectDB($config_data);

//
// start loading NSRLfile.txt
//
//
//
$handle = fopen("$file1", "r");
if(!$handle) {
	$xml = c_initiate_xml($config_data);
	$xml_error = $xml->addchild('error');
	$xml_error->addchild('code', '500');
	$xml_error->addchild('description', 'Could not establish a read handle to '.$file);
	c_announce($xml);
}

while (!feof($handle)) {
	
	$line = fgetcsv($handle);
	//print_r($line);
	
	$query = "INSERT INTO NSRLFile VALUES ('$line[0]', '$line[1]', '$line[2]', '$line[3]', '$line[4]', '$line[5]', '$line[6]', '$line[7]')";
	
	mysqli_query($db_link, $query);

}

//
// start loading NSRLMfg.txt
//
//
//
$handle = fopen("$file2", "r");
if(!$handle) {
	$xml = c_initiate_xml($config_data);
	$xml_error = $xml->addchild('error');
	$xml_error->addchild('code', '500');
	$xml_error->addchild('description', 'Could not establish a read handle to '.$file);
	c_announce($xml);
}

while (!feof($handle)) {
	
	$line = fgetcsv($handle);
	//print_r($line);
	
	$query = "INSERT INTO NSRLMfg VALUES ('$line[0]', '$line[1]')";
	
	mysqli_query($db_link, $query);

}

//
// start loading NSRLOS.txt
//
//
//
$handle = fopen("$file3", "r");
if(!$handle) {
	$xml = c_initiate_xml($config_data);
	$xml_error = $xml->addchild('error');
	$xml_error->addchild('code', '500');
	$xml_error->addchild('description', 'Could not establish a read handle to '.$file);
	c_announce($xml);
}

while (!feof($handle)) {
	
	$line = fgetcsv($handle);
	//print_r($line);
	
	$query = "INSERT INTO NSRLOS VALUES ('$line[0]', '$line[1]', '$line[2]', '$line[3]')";
	
	mysqli_query($db_link, $query);

}

//
// start loading NSRLProd.txt 
//
//
//
$handle = fopen("$file4", "r");
if(!$handle) {
	$xml = c_initiate_xml($config_data);
	$xml_error = $xml->addchild('error');
	$xml_error->addchild('code', '500');
	$xml_error->addchild('description', 'Could not establish a read handle to '.$file);
	c_announce($xml);
}


while (!feof($handle)) {
	
	$line = fgetcsv($handle);
	//print_r($line);
	
	$query = "INSERT INTO NSRLProd VALUES ('$line[0]', '$line[1]', '$line[2]', '$line[3]', '$line[4]', '$line[5]', '$line[6]')";
	
	mysqli_query($db_link, $query);

}

?>